﻿namespace FiftyOne.Foundation.Image
{
    using System;
    using System.Collections.Generic;
    using System.Drawing.Imaging;

    internal class Support
    {
        private static List<ColorsToBitsPerPixel> _colorTable = null;
        private static Dictionary<ImageFormat, string> _contentTypes = null;

        static Support()
        {
            InitColorTable();
            InitContentTypes();
        }

        private static void AddPair(List<ColorsToBitsPerPixel> colorTable, int bitsPerPixel, long colors)
        {
            colorTable.Add(new ColorsToBitsPerPixel(bitsPerPixel, colors));
        }

        internal static int GetBitsPerPixel(long colors)
        {
            foreach (ColorsToBitsPerPixel pixel in _colorTable)
            {
                if (colors <= pixel.Colors)
                {
                    return pixel.BitsPerPixel;
                }
            }
            return 8;
        }

        internal static string GetContentType(ImageFormat format)
        {
            if ((format != null) && _contentTypes.ContainsKey(format))
            {
                return _contentTypes[format];
            }
            return null;
        }

        private static void InitColorTable()
        {
            _colorTable = new List<ColorsToBitsPerPixel>();
            AddPair(_colorTable, 1, 2L);
            AddPair(_colorTable, 2, 4L);
            AddPair(_colorTable, 3, 8L);
            AddPair(_colorTable, 4, 0x10L);
            AddPair(_colorTable, 5, 0x20L);
            AddPair(_colorTable, 6, 0x40L);
            AddPair(_colorTable, 7, 0x80L);
            AddPair(_colorTable, 8, 0x100L);
            AddPair(_colorTable, 9, 0x200L);
            AddPair(_colorTable, 10, 0x400L);
            AddPair(_colorTable, 11, 0x800L);
            AddPair(_colorTable, 12, 0x1000L);
            AddPair(_colorTable, 13, 0x2000L);
            AddPair(_colorTable, 14, 0x4000L);
            AddPair(_colorTable, 15, 0x8000L);
            AddPair(_colorTable, 0x10, 0x10000L);
            AddPair(_colorTable, 0x11, 0x20000L);
            AddPair(_colorTable, 0x12, 0x40000L);
            AddPair(_colorTable, 0x13, 0x80000L);
            AddPair(_colorTable, 20, 0x100000L);
            AddPair(_colorTable, 0x15, 0x200000L);
            AddPair(_colorTable, 0x16, 0x400000L);
            AddPair(_colorTable, 0x17, 0x800000L);
            AddPair(_colorTable, 0x18, 0x1000000L);
            AddPair(_colorTable, 0x19, 0x2000000L);
            AddPair(_colorTable, 0x1a, 0x4000000L);
            AddPair(_colorTable, 0x1b, 0x8000000L);
            AddPair(_colorTable, 0x1c, 0x10000000L);
            AddPair(_colorTable, 0x1d, 0x20000000L);
            AddPair(_colorTable, 30, 0x40000000L);
            AddPair(_colorTable, 0x1f, 0x80000000L);
            AddPair(_colorTable, 0x20, 0x100000000L);
        }

        internal static void InitContentTypes()
        {
            _contentTypes = new Dictionary<ImageFormat, string>();
            _contentTypes.Add(ImageFormat.Png, "image/png");
            _contentTypes.Add(ImageFormat.Gif, "image/gif");
            _contentTypes.Add(ImageFormat.Jpeg, "image/jpeg");
            _contentTypes.Add(ImageFormat.Bmp, "image/bmp");
            _contentTypes.Add(ImageFormat.Tiff, "image/tiff");
        }

        internal protected class ColorsToBitsPerPixel
        {
            private int _bitsPerPixel;
            private long _colors;

            protected internal ColorsToBitsPerPixel(int bitsPerPixel, long colors)
            {
                this._bitsPerPixel = bitsPerPixel;
                this._colors = colors;
            }

            protected internal int BitsPerPixel
            {
                get
                {
                    return this._bitsPerPixel;
                }
            }

            protected internal long Colors
            {
                get
                {
                    return this._colors;
                }
            }
        }
    }
}

